package com.ruoyi.gigi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gigi.course.domain.Course;
import com.ruoyi.gigi.course.service.ICourseService;
import com.ruoyi.gigi.gigi_class.service.IGigiClassService;
import com.ruoyi.gigi.user.domain.User;
import com.ruoyi.gigi.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 课程管理Controller
 *
 * @author gigi
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/gigi.course/course")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService courseService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IGigiClassService classService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        list.forEach(e -> {
            User user = userService.selectUserById(e.getUserId());
            if (Objects.nonNull(user)) {
                e.setUserName(user.getName());
            }
            List<String> classes = Arrays.stream(StringUtils.split(e.getClassId(), ",")).collect(Collectors.toList());
            List<String> classNames = classes.stream().map(i -> {
                return classService.selectGigiClassById(Long.valueOf(i)).getName();
            }).collect(Collectors.toList());
            e.setClassName(org.apache.commons.lang3.StringUtils.join(classNames, ","));
        });
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course) {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Course course = courseService.selectCourseById(id);
        course.setClassesId(Arrays.stream(StringUtils.split(course.getClassId(), ",")).map(Long::valueOf).collect(Collectors.toList()));
        return success(course);
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course) {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}
